package cn.topevery.garden.wapi.controller;

import cn.topevery.garden.dal.logic.PersonDutyLogic;
import cn.topevery.garden.entity.dto.PersonDutyDTO;
import cn.topevery.garden.entity.query.PersonDutyQuery;
import cn.topevery.garden.entity.vo.PersonDutyVO;
import cn.topevery.garden.entity.po.PersonDuty;
import hw.topevery.basis.entity.po.SysUser;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.util.ExcelUtil;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.github.dozermapper.core.Mapper;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 请假/加班
 */
@Api(tags = "请假/加班")
@RestController
@RequestMapping("/personDuty")
public class PersonDutyController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    PersonDutyLogic personDutyLogic;


    /**
     * 获取请假/加班分页列表
     *
     * @param query
     * @return
     */
    @ApiOperation(value = "获取请假/加班页列表")
    @PostMapping("/listPage")
    public JsonResult<DataPager<PersonDutyVO>> listPage(@RequestBody PersonDutyQuery query) {
        return JsonResult.ok(personDutyLogic.listPage(query));
    }

    @ApiOperation("新增/编辑 请假/加班信息")
    @PostMapping("/saveOrUpdate")
    public JsonResult<Boolean> saveOrUpdate(@Valid @RequestBody PersonDutyDTO dto) {
        SysUser user = getUser();
        if(StringUtils.isBlank(dto.getEntityId())){
            dto.setEntityId(UUID.randomUUID().toString());
            dto.setUserId(user.userId);
            dto.setUserName(user.userName);
            dto.setReqDate(LocalDateTime.now());
            dto.setAuditResult(0); // 待审核
        }
        // 审核
//        if(!dto.getUserId().equals(user.userId))
        {
            dto.setAuditDate(LocalDateTime.now());
            dto.setAuditUserId(user.userId);
            dto.setAuditUserName(user.userName);
        }
        personDutyLogic.saveOrUpdate(getUser().id.toString(),mapper.map(dto, PersonDuty.class));
        return JsonResult.ok(true);
    }

    @ApiOperation("通过Id获取详情")
    @GetMapping("/getInfoById")
    public JsonResult<PersonDutyVO> getInfoById(@RequestParam Integer id) {
        PersonDuty entity = personDutyLogic.getEntity(id);
        PersonDutyVO vo= mapper.map(entity, PersonDutyVO.class);
        return JsonResult.ok(vo);
    }

    @ApiOperation("根据自增ID批量删除")
    @PostMapping("/deleteBatchById")
    public JsonResult<Boolean> deleteBatchById(@RequestBody Integer[] ids) {
        personDutyLogic.deleteBatchById(getUser().id.toString(),ids);
        return JsonResult.ok(true);
    }

    @ApiOperation("导出请假/加班信息")
    @GetMapping("/export")
    public void export(PersonDutyQuery query, HttpServletResponse response) {
        List<PersonDutyVO> list = personDutyLogic.listPage(query).records;
        ExcelUtil.exportExcel(list, "请假/加班", "sheet1", PersonDutyVO.class, "请假/加班", response);
    }

}
